<?php

namespace app\api\controller;

use app\admin\model\Users;
use app\admin\model\Car;
use app\common\controller\Api;
use think\DB;

class Carusers extends Api {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize() {
        parent::_initialize();

        if (empty($this->user_id)) {
            $this->error("请登录!", '', 110, 'json', ['statuscode' => 200]);
        }
    }

    //获取用户信息
    public function userInfo() {
        $userInfo = db('Users')->where(array('user_id' => $this->user_id))->find();

        if ($userInfo) {

            $userInfo['bri'] = '';
            if (!empty($userInfo['brithday'])) {
                $bri = strtotime($userInfo['brithday']);

                $userInfo['bri'] = date('Y-m-d', $bri);
            }
        }
        $this->success('获取成功', $userInfo);
    }

    public function setUserInfo() {
        $real_name = $this->request->param('real_name');
        $sex = $this->request->param('sex', 1, 257);
        $company = $this->request->param('company');
        $company_address = $this->request->param('company_address');
        $position = $this->request->param('position');
        $company_introduction = $this->request->param('company_introduction');
        $wx_number = $this->request->param('wx_number');
        $birthday = $this->request->param('birthday');
        $phone = $this->request->param('phone');
//        $constellation = I('constellation');

        $msg = $this->request->param('msg');


        $data = [];
        if (!empty($phone)) {
            if (checkPhone($phone)) {
                $data['phone'] = $phone;
            } else {
                $this->error('手机号格式不正确');
            }
        }
        
        if ($sex != -1) {
            $data['sex'] = $sex;
        }

        if (!empty($real_name)) {
            $data['real_name'] = $real_name;
        }
        if (!empty($company)) {
            $data['company'] = $company;
        }
        if (!empty($company_address)) {
            $data['company_address'] = $company_address;
        }
        if (!empty($position)) {
            $data['position'] = $position;
        }
        if (!empty($company_introduction)) {
            $data['company_introduction'] = $company_introduction;
        }
        if (!empty($wx_number)) {
            $data['wx_number'] = $wx_number;
        }

        if (!empty($msg)) {
            $data['msg'] = $msg;
        }

        if (!empty($birthday)) {

            if (strtotime($birthday)) {
                $data['brithday'] = $birthday;
            }
            $data['constellation'] = birthext($birthday);
        }
//        if (!empty($constellation)) {
//            $data['constellation'] = $constellation;
//        }

        $userM = new Users();

        $res = $userM->save($data, ['user_id' => $this->user_id]);
//        $res = M('Users')->where(['user_id' => $this->user_id])->save($data);

        $this->success('更新成功', $res);
    }

    public function getMyCarInfo() {
        $car_info = db('Users')->where(array('user_id' => $this->user_id))->find();
        $car = db('Car')
                ->where(['user_id' => $this->user_id])
                ->select();


        if ($car_info) {
            $car_info['car'] = [];

            if ($car) {
                $ad_url = [];

                $car_id=0;
                $i = 1;
                foreach ($car as &$val) {
                    $car_id = $val['car_id'];
                    $val['no'] = $i;
                    $where = "(ad_position = '1' and min <='" . $val['car_id'] . "' and max>='" . $val['car_id'] . "' and type='1') or (ad_position = '1' and min<='" . $val['id'] . "' and max>='" . $val['id'] . "' and type='2')";
                    $ad_list = db::query('select * from fa_ad where ' . $where);
                    foreach ($ad_list as &$row) {
                        $item['url'] = 'https://lc.feiyouce.com' . $row['img_url'];
                        $item['ad_id'] = $row['ad_id'];
                        $ad_url[] = $item;
                    }


                    $i++;
                }
                unset($val);

                $car_info['car'] = $car;
                $car_info['car_id'] = $car_id;
                $car_info['car_num'] = count($car);
                $car_info['car_str'] = $car[0]['car_str'];
                $car_info['car_number'] = $car[0]['car_number'];

                $car_info['hide_ad'] = 0;
                if (empty($ad_url)) {
                    $car_info['hide_ad'] = 1;
                }

                $car_info['ad'] = array_unique_fb($ad_url);
            }

            $car_info['bri'] = '';
            if (!empty($car_info['brithday'])) {
                $bri = strtotime($car_info['brithday']);

                $car_info['bri'] = date('m-d', $bri);
                $car_info['brithday'] = date('Y-m-d', $bri);
            }

            $this->success('获取成功', $car_info);
        } else {

            $this->error('没有找到数据');
        }
    }

    public function editCarNo() {
        $id = $this->request->param('id', 0, 257);
        $car_str = $this->request->param('car_str');
        $car_number = $this->request->param('car_number');


        if ($id == 0) {
            $this->error('请选择要编辑的车牌');
        }

        if (!empty($car_str)) {
            $data['car_str'] = $car_str;
        }

        if (!empty($car_number)) {
            $car_number = strtoupper($car_number);
            $data['car_number'] = $car_number;
        }

        if ($data) {
            $carM = new Car();

            $res = $carM->save($data, ['id' => $id]);
        }

        $this->success('更新成功', $res);
    }

    public function delCar() {
        $id = $this->request->param('id', 0, 257);
        if ($id == 0) {
            $this->error('请选择要删除的车');
        }

        $carM = new Car();

        $res = $carM->where(['id' => $id])->delete();

        $this->success('删除成功', $res);
    }

}
